package com.myschool.util;


import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BaseDao {

    public static Connection getConnection(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String USER_NAME = "root";
            String  PWD = "123456";
            String URL = "jdbc:mysql://localhost:3306/testzr?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false";
            Connection con = DriverManager.getConnection(URL,USER_NAME,PWD);
            return con;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void closeDB(ResultSet r, PreparedStatement ps,Connection con){
        if(r!=null){
            try {
                r.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static int executeUpdate(String sql,Object...parms){
        Connection con = getConnection();
        try {
            PreparedStatement ps = con.prepareStatement(sql);
            for (int i=0;i<parms.length;i++){
                ps.setObject(i+1,parms[i]);
            }
            int a = ps.executeUpdate();
            closeDB(null,ps,con);
            return a;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static <T>List<T> show(String sql,Class classz,Object...parms){
        //创建集合
        List<T> list = new ArrayList<>();
        //获取连接
        Connection con = getConnection();
        try {
            //获取语句处理对象
            PreparedStatement ps = con.prepareStatement(sql);
            //将所需参数插入语句
//            for (int i=0;i<parms.length;i++){
//                ps.setString(i+1,parms[i]+"");
//            }
            //接收结果
            ResultSet rs = ps.executeQuery();
            //获取需查类对应有几个属性
            Field[] fs = classz.getDeclaredFields();
            //循环读取每一行的值
            while (rs.next()){
                //创建对应对象
                T obj = (T) classz.newInstance();
                //给每一个属性赋值
                for(int i=0;i<fs.length;i++){
//                    放开属性权限
                    fs[i].setAccessible(true);
                    //获得读取数据
                    ResultSetMetaData rsmd = rs.getMetaData();
                    String x = rsmd.getColumnTypeName(i+1);
                    String name = fs[i].getName();
                    if(x.equals("DATETIME")){
                        Date d = new Date(rs.getTimestamp(name).getTime());
                        fs[i].set(obj,d);
                    } else {
                        Object o = rs.getObject(name);
                        //给对应属性赋值
                        fs[i].set(obj,o);
                    }

                }
                System.out.println();
                //加入集合
                list.add(obj);
            }
            //释放资源
            closeDB(rs,ps,con);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
       return list;
    }

}
